
var imgArray = new Array();

/*添加图片*/
function addImages(url) {
    var tempId = getTimestamp();
    var html = " <div id="+tempId+">";
    if(imgArray.length>0){
        html = html+"<label class=\"col-sm-2 control-label\"></label>"
    }

    html = html + "    <a class=\"fancybox\" href="+url+">" +
               "        <img alt=\"image\" style='width: 15%' src="+url+" />" +
               "    </a>" +
               "    <button class=\"btn btn-sm btn-danger\" img-url="+url+" onclick=\"deleteImg(this,'"+tempId+"')\">" +
               "        <strong>删除</strong>" +
               "    </button>" +
               " </div>";
    //添加页面图片显示
    $('#upload-list').append(html);

    imgArray.push(url);

    console.log(JSON.stringify(imgArray));

}

//删除图片
function deleteImg(obj,tempId) {
    $('#'+tempId).remove();
    var imgUrl = $(obj).attr('img-url');
    imgArray.removeByValue(imgUrl);
    console.log(imgArray);
}

//添加商品
function addGiveaway() {
    var giveaway_no = $('#giveaway_no').val();
    if(checkIsNull(giveaway_no)){
        alertTips("请填写赠品物料编号！");
        return false;
    }

    var giveaway_name = $('#giveaway_name').val();
    if(checkIsNull(giveaway_name)){
        alertTips("请填写赠品物料名称！");
        return false;
    }
    // var sale_city = $("#sale_city option:selected").val();

    if(imgArray.length<1){
        alertTips("请上传赠品图片！");
        return false;
    }
    var giveaway_img = imgArray[0];
    var giveaway_imgs = JSON.stringify(imgArray);

    var giveaway_unit_no = $("#giveaway_unit option:selected").val();
    if(checkIsNull(giveaway_unit_no)){
        alertTips("请选择赠品单位！");
        return false;
    }

    var giveaway_brand_no = $("#giveaway_brand option:selected").val();
    if(checkIsNull(giveaway_brand_no)){
        alertTips("请选择赠品品牌！");
        return false;
    }

    var giveaway_type_no = $("#giveaway_type option:selected").val();
    if(checkIsNull(giveaway_type_no)){
        alertTips("请选择赠品类型！");
        return false;
    }

    var giveaway_specifications_no = $("#giveaway_specifications option:selected").val();
    if(checkIsNull(giveaway_specifications_no)){
        alertTips("请选择赠品规格！");
        return false;
    }

    var desc = $('#desc-content').val();

    $.ajax({
        type: "post",
        url: '/goods/addGiveaway',
        dataType: "json",
        data: {
            'giveaway_no' : giveaway_no,
            'giveaway_name' : giveaway_name,
            'giveaway_img' : giveaway_img,
            'giveaway_imgs' : giveaway_imgs,
            'giveaway_unit_no' : giveaway_unit_no,
            'giveaway_brand_no' : giveaway_brand_no,
            'giveaway_type_no' : giveaway_type_no,
            'giveaway_specifications_no' : giveaway_specifications_no,
            'desc' : desc
        },
        success: function(resp){
            console.log(resp);
            if(resp.result==='success'){
                alertTips("新增赠品成功！");
                location.href='/goods/toGiveawayList';
            }else{
                alertTips("新增赠品异常！");
            }
        }
    });


}

